﻿using System;
using System.Windows;
using System.Windows.Forms;
using project.generator.Properties;
using JUtil;
using JUtil.Path;
using JUtil.ResourceManagement;

namespace projectgenerator
{
    /// <summary>
    /// MainWindow.xaml 的互動邏輯
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            try
            {
                InitializeComponent();

                this.DataContext = Settings.Default;
                txtProjectName_TextChanged(null, null);
                txtProjectDirectory_TextChanged(null, null);
            }
            catch (Exception ex)
            {
            	Log.ReportError(ex);
            }
        }

        void txtProjectName_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            try
            {
                string projectDirectory = txtProjectDirectory.Text;
                string projectName = txtProjectName.Text;

                if (string.IsNullOrEmpty(projectName))
                {
                    btnBrowse.IsEnabled = false;

                    txtProjectDirectory.Text = string.Empty;
                }
                else
                {
                    btnBrowse.IsEnabled = true;

                    if (string.IsNullOrEmpty(projectDirectory))
                        return;

                    projectDirectory = System.IO.Path.GetDirectoryName(projectDirectory);
                    string path = File.GetAbsolutePath(projectDirectory, projectName);
                    txtProjectDirectory.Text = path;
                }
            }
            catch (Exception ex)
            {
            	Log.ReportError(ex);
            }
        }

        private void BrowserDirectory(object sender, RoutedEventArgs e)
        {
            try
            {
                FolderBrowserDialog dialog = new FolderBrowserDialog();
                string projectDirectory = txtProjectDirectory.Text;
                if (string.IsNullOrEmpty(projectDirectory))
                {
                    dialog.SelectedPath = Settings.Default.ProjectsRoot;
                }
                else
                {
                    projectDirectory = System.IO.Path.GetDirectoryName(projectDirectory);
                    dialog.SelectedPath = projectDirectory;
                }

                if (System.Windows.Forms.DialogResult.OK == dialog.ShowDialog())
                {
                    string path = File.GetAbsolutePath(dialog.SelectedPath, txtProjectName.Text);
                    txtProjectDirectory.Text = path;
                }
            }
            catch (Exception ex)
            {
            	Log.ReportError(ex);
            }
        }

        private void txtProjectDirectory_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            try
            {
                string projectDirectory = txtProjectDirectory.Text;

                if (string.IsNullOrEmpty(projectDirectory))
                {
                    btnGenerate.IsEnabled = false;
                }
                else
                {
                    btnGenerate.IsEnabled = true;
                }
            }
            catch (Exception ex)
            {
                Log.ReportError(ex);
            }
        }

        private void Generate(object sender, RoutedEventArgs e)
        {
            try
            {
                string path = txtProjectDirectory.Text;
                ProjectGenerator pg = new ProjectGenerator(path);
                pg.Build();
            }
            catch (Exception ex)
            {
            	Log.ReportError(ex);
            }
        }


    } // end of MainWindow
}
